package com.rabbitmq.extension.controller;

import com.rabbitmq.extension.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;
//    @Resource(name = "confirmRabbitTemplate")
//    private RabbitTemplate confirmRabbitTemplate;
    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;
//    @RequestMapping("/confirm")
//    public String confirm(){
//        CorrelationData correlationData=new CorrelationData("1");
//       confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm111","测试Confirm确认模式...",correlationData);
//        return "Confirm确认发送成功";
//
//    }

    @RequestMapping("/retry")
    public String retry(){
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"retry","测试重试机制...");
        return "发送成功";
    }
    @RequestMapping("/delay2")
    public String delay2(){
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delayed",
                "延迟队列消息 20s..."+new Date(),message -> {
            message.getMessageProperties().setDelayLong(20000L);
            return message;
                });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delayed", "延迟队列消息 10s..."+new Date(),
                message -> {
            message.getMessageProperties().setDelayLong(10000L);
            return message;
                });
        return "消息发送成功";
    }

    @RequestMapping("/delay")
    public String delay(){
        System.out.println("开始执行用死性队列+ttl的方式模拟延迟队列");
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal",
                "用死性队列+ttl的方式模拟延迟队列 20s..."+new Date(),message -> {
                    message.getMessageProperties().setExpiration("20000");
                    return message;
                });
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal", "用死性队列+ttl的方式模拟延迟队列" +
                        " 10s..."+new Date(),
                message -> {
                    message.getMessageProperties().setExpiration("10000");
                    return message;
                });
        return "消息发送成功";
    }
    @RequestMapping("/send")
//    @Transactional
    public String send(){
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"事务测试1...");
        int a=5/0;
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"事务测试2...");
        return "发送成功";
    }

    @RequestMapping("/qos")
    public String qos(){
        for(int i=0;i<20;i++){
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","测试限流..."+i);
        }
        return "发送成功!";
    }

    @RequestMapping("/cluster")
    public String cluster(){
        rabbitTemplate.convertAndSend("","cluster.queue","测试给仲裁队列加一个统一的入口...");
        return "发送成功";
    }


}
